/**

   chunk.c
   =======

   Chunk management

**/


/**

   Includes
   --------

   - stdlib   : malloc, free
   - uuid.h   : struct uuid
   - chunk.h  : self header

**/


#include <stdlib.h>
#include <uuid.h>
#include "chunk.h"


/**

   Function: struct chunk* chunk_create(void)
   ------------------------------------------

   Create a chunk

**/


struct chunk* chunk_create(void)
{
  struct uuid* uuid;
  struct chunk* c;
  int i;

  /* Create uuid */
  uuid = uuid_create();
  if (uuid == NULL)
    {
      return NULL;
    }
  
  /* Allocate structure */
  c = (struct chunk*)malloc(sizeof(struct chunk));
  if (c == NULL)
    {
      uuid_delete(uuid);
      return NULL;
    }

  /* Fill fields */
  for(i=0;i<CHUNK_REPLICA;i++)
    {
      c->srv[i] = NULL;
    }
  c->uuid = uuid;
  c->next = NULL;

  /* Return */
  return c;

}


/**

   Function: void chunk_delete(struct chunk* c)
   --------------------------------------------

   Delete a chunk and its successors.
   Recursive function

**/


void chunk_delete(struct chunk* c)
{
  if (c != NULL)
    {
      /* Free structures */
      uuid_delete(c->uuid);
      free(c);
    }

  return;
}
